package com.itita.ww2.handler.requesthandler.account;

import java.sql.SQLException;

import com.itita.ww2.core.MessageHandler;
import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.game.user.UserService;
import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.puredb.model.UserFcm;
import com.itita.ww2.utils.TimeUtil;
import com.itita.ww2.utils.handler.IRequestOper;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;

public class BindFCMToken implements IRequestOper {

	@Override
	public boolean execute(String requestCMD, User user, ISFSObject params) throws WW2Exception, SQLException {
		UserProfile userProfile = UserService.getInstance().getUserProfile(user);
		String token = params.getUtfString("token");

		UserFcm userFcm = UserFcm.loadByUid(userProfile.getUid());
		if (userFcm != null) {
			userFcm.setFirebaseToken(token);
			userFcm.setBindTime(TimeUtil.getUtcNowMillis());
			userFcm.update();
		} else {
			userFcm = new UserFcm();
			userFcm.setUid(userProfile.getUid());
			userFcm.setFirebaseToken(token);
			userFcm.setBindTime(TimeUtil.getUtcNowMillis());
			userFcm.insert();
		}

		MessageHandler.getInstance().sendMsg(requestCMD, null, user, null, true);
		return true;
	}

}
